package net.quanter.samples.nacos.consumer;

import net.quanter.sample.nacos.api.UserService;
import net.quanter.sample.nacos.dto.ResultDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ImportResource;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author xiaojing
 */
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
@ImportResource({"classpath:dubbo.xml"})
public class NacosConsumerApplication {

    @LoadBalanced
    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }

    public static void main(String[] args) {
        SpringApplication.run(NacosConsumerApplication.class, args);
    }

    @RestController
    public class TestController {

        @Value("${spring.application.name}")
        String applicationName;

        //dubbo-client
        @Resource
        UserService helloService;

        //feign-client
//        @Resource
        UserServiceFeign helloServiceFeign;

        @Resource
        UserServiceFeignExtend helloServiceFeignExtend;

        private final RestTemplate restTemplate;

        @Autowired
        public TestController(RestTemplate restTemplate) {this.restTemplate = restTemplate;}

        @GetMapping(value = "/echo/{str}")
        public Map echo(
                @PathVariable String str,
                HttpServletRequest request) throws UnknownHostException {
            InetAddress address = InetAddress.getLocalHost();
            Map<String,Object> map = new HashMap<>();
            map.put("微服务名",applicationName);
            map.put("服务端ip地址",address.getHostAddress());
            map.put("请求段ip地址",request.getRemoteAddr());
            Map<String,String> result = restTemplate.getForObject("http://nacos-provider-sample/echo/" + str, Map.class);
            map.put("请求服务结果",result);
            return map;
        }

        @GetMapping(value = "/dubbo/{id}")
        public ResultDTO dubbo(@PathVariable Integer id) {
            ResultDTO providerResult = helloService.get(id);
            ResultDTO result = ResultDTO.success(providerResult,"我利用dubbo方式调用provider");
            return result;
        }

        @GetMapping(value = "/resttemplate/{id}")
        public ResultDTO resttemplate(@PathVariable Integer id) {
            ResultDTO<String> providerResult = restTemplate.getForObject("http://nacos-provider-sample/user/"+id, ResultDTO.class);
            ResultDTO<String> result = ResultDTO.success(providerResult,"我利用springcloud的resttemplate方式调用provider");
            return result;
        }

        @GetMapping(value = "/feign/{id}")
        public ResultDTO feign(@PathVariable Integer id) {
            ResultDTO<UserServiceFeign.UserDTO> providerResult = helloServiceFeign.get(id);
            ResultDTO result = ResultDTO.success(providerResult,"我利用springcloud的feign(伪RPC)方式调用provider,因为底层是利用基于http的json，所以我在consumer端可以使用自定义的返回类型");
            return result;
        }

        @GetMapping(value = "/feignextend/{id}")
        public ResultDTO feignExtend(@PathVariable Integer id) {
            ResultDTO providerResult = helloServiceFeignExtend.get(id);
            ResultDTO result = ResultDTO.success(providerResult,"我利用基于接口继承的feign(伪RPC)方式调用provider，这样consumer端不需要复制provider的api接口里的代码");
            return result;
        }
    }

}